﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Domain.Report;
using S4Store.Domain;
using S4Store.Domain.CarKpi;
using S4Store.Domain.CommonSellerKpi;

namespace S4Store.Service
{
    public class ReportService : BaseService
    {
        #region 车型
        public CarSaleReport GetSaleReport(int year,int month,int storeid)
        {
            var helper = getCarHelper(year, month, storeid);
            return helper.StoreReport;
        }

        public CarBrandSaleReport GetCarBrandSaleReport(int year, int month, int storeid, int carbrandid)
        {

            var helper = getCarHelper(year, month, storeid);
            var result= helper.CarBrandReports.FirstOrDefault(t=>t.CarBrand.Id==carbrandid);
            if (result == null)
            {
                DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
                CarService CarService = new Service.CarService();
                CarKpiService CarKpiService = new CarKpiService();
                StoreService StoreService = new StoreService();
                this.GetTranstion().Inject(CarService);
                this.GetTranstion().Inject(CarKpiService);
                this.GetTranstion().Inject(StoreService);

                result = new CarBrandSaleReport
                {
                    Year = year,
                    Month = month,
                     CarBrand=CarService.GetCarBrandQuery().FirstOrDefault(x=>x.Id==carbrandid),
                    CarKpiMonthConfig = CarKpiService.GetCarKpiMonthConfig(year, month, storeid, true),
                     CarModelSaleReports=new List<CarModelSaleReport>(),
                    Day = dt.Day,
                    Store = StoreService.GetQuery().FirstOrDefault(t => t.Id == storeid),
                     SellerPerformanceStandard= GetSellerPerformanceStandard(year,month,storeid)
                };
            }
            return result;
        }

        public CarModelSaleReport GetCarModelSaleReport(int year, int month, int storeid, int carmodelid)
        {
            var helper = getCarHelper(year, month, storeid);
            var result = helper.CarModelReports.FirstOrDefault(t => t.CarModel.Id == carmodelid);
            if (result == null)
            {
                DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
                CarService CarService = new Service.CarService();
                CarKpiService CarKpiService = new CarKpiService();
                StoreService StoreService = new StoreService();
                this.GetTranstion().Inject(CarService);
                this.GetTranstion().Inject(CarKpiService);
                this.GetTranstion().Inject(StoreService);

                result = new CarModelSaleReport
                {
                    Year = year,
                    Month = month, 
                    Day = dt.Day,
                    CarModel = CarService.GetCarModelQuery().FirstOrDefault(x => x.Id == carmodelid),
                     CarKpiDayDatas=new List<CarKpiDayData>(),
                    CarKpiMonthConfig = CarKpiService.GetCarKpiMonthConfig(year, month, storeid, true),
                    CarModelMonthConfig = CarKpiService.GetCarModelMonthConfig(year, month, storeid,carmodelid, true),
                    StoreReport = helper.StoreReport,
                    Store = StoreService.GetQuery().FirstOrDefault(t => t.Id == storeid),
                    SellerPerformanceStandard = GetSellerPerformanceStandard(year, month, storeid)
                };
            }
            return result;
        }
        #endregion

        public SellerPerformanceStandard SaveSellerPerformanceStandard(SellerPerformanceStandard model)
        {
            var standard = this.GetQuery<SellerPerformanceStandard>().FirstOrDefault(t => t.Id==model.Id);
            if (standard != null)
            {
                standard.SCSJLV = model.SCSJLV;
                standard.SCSJCJLV = model.SCSJCJLV;
                standard.DJKHCJLV = model.DJKHCJLV;
                standard.SYKHZHLV = model.SYKHZHLV;
                standard.CXJGCJLV = model.CXJGCJLV;
                standard.SCLDDJLV = model.SCLDDJLV;
                standard.AKXQWZFXLV = model.AKXQWZFXLV;
                standard.STMXBGTXLV = model.STMXBGTXLV;
                standard.DDJCZHLV = model.DDJCZHLV;
                standard.ESCPGLV = model.ESCPGLV;
                standard.ZHLV = model.ZHLV;
                standard.BXLV = model.BXLV;
                standard.AJCJLV = model.AJCJLV;
                standard.TCCXSLV = model.TCCXSLV;
                standard.YXZHCXSLV = model.YXZHCXSLV;
                standard.XCYBSXLV = model.XCYBSXLV;
                standard.ECYYCGLV = model.ECYYCGLV;
                standard.LKHTJCGLV = model.LKHTJCGLV;
                standard.LKHTJCGSL = model.LKHTJCGSL;
                standard.SCLDPS = model.SCLDPS;
                standard.SCLDHS = model.SCLDHS;
                standard.PJDCZHE = model.PJDCZHE;
                standard.PJDCBXE = model.PJDCBXE;
                standard.DHZLDDLV = model.DHZLDDLV;
            }
            return standard;
        }

        public SellerPerformanceStandard GetSellerPerformanceStandard(int year, int month,int storeid)
        {
            var model= this.GetQuery<SellerPerformanceStandard>().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month&&t.StoreId==storeid);
            if (model == null)
            {
                 model = new SellerPerformanceStandard();
                model.Month=new DateTime(year,month,1);
                model.StoreId = storeid;
                var date = new DateTime(year, month, 1).AddMonths(-1);
                var lyear = date.Year;
                var lmonth = date.Month;
                var lastmodel = this.GetQuery<SellerPerformanceStandard>().FirstOrDefault(t => t.Month.Year == lyear && t.Month.Month == lmonth && t.StoreId == storeid);
                if (lastmodel != null)
                {
                    model.SCSJLV = lastmodel.SCSJLV;
                    model.SCSJCJLV = lastmodel.SCSJCJLV;
                    model.DJKHCJLV = lastmodel.DJKHCJLV;
                    model.SYKHZHLV = lastmodel.SYKHZHLV;
                    model.CXJGCJLV = lastmodel.CXJGCJLV;
                    model.SCLDDJLV = lastmodel.SCLDDJLV;
                    model.AKXQWZFXLV = lastmodel.AKXQWZFXLV;
                    model.STMXBGTXLV = lastmodel.STMXBGTXLV;
                    model.DDJCZHLV = lastmodel.DDJCZHLV;
                    model.ESCPGLV = lastmodel.ESCPGLV;
                    model.ZHLV = lastmodel.ZHLV;
                    model.BXLV = lastmodel.BXLV;
                    model.AJCJLV = lastmodel.AJCJLV;
                    model.TCCXSLV = lastmodel.TCCXSLV;
                    model.YXZHCXSLV = lastmodel.YXZHCXSLV;
                    model.XCYBSXLV = lastmodel.XCYBSXLV;
                    model.ECYYCGLV = lastmodel.ECYYCGLV;
                    model.LKHTJCGLV = lastmodel.LKHTJCGLV;
                    model.LKHTJCGSL = lastmodel.LKHTJCGSL;
                    model.SCLDPS = lastmodel.SCLDPS;
                    model.SCLDHS = lastmodel.SCLDHS;
                    model.PJDCZHE = lastmodel.PJDCZHE;
                    model.PJDCBXE = lastmodel.PJDCBXE;
                }
                this.GetSet<SellerPerformanceStandard>().Add(model);
            }
            return model;
        }

        #region 人员
        public SellerReport GetSellerReport(int year, int month, int storeid, EmployeeType type = EmployeeType.FieldSaler)
        {
            
            if (type == EmployeeType.FieldSaler)
            {
                SellerHelper helper = getAllpersonbrandReport(year, month, storeid);
                return helper.StoreReport;
            }
            else if (type == EmployeeType.TelSaler)
            {
                SellerHelper helper = getAlltelpersonbrandReport(year, month, storeid);
                return helper.StoreReport;
            }
            else
            {
                SellerHelper helper = getFullpersonbrandReport(year, month, storeid);
                return helper.StoreReport;
            }
        }
        
        public SellerPersonReport GetSellerPersonReport(int year, int month, int storeid, int sellerid)
        {
            EmployeeService EmployeeService = new Service.EmployeeService();
            this.GetTranstion().Inject(EmployeeService);
            var seller = EmployeeService.GetQuery().FirstOrDefault(t => t.Id == sellerid);
            SellerPersonReport result = null;
            if (seller.Type == EmployeeType.FieldSaler)
            {
                SellerHelper helper = getAllpersonbrandReport(year, month, storeid);
                result = helper.SellerReports.FirstOrDefault(t => t.Seller.Id == sellerid);                
            }
            else if (seller.Type == EmployeeType.TelSaler)
            {
                SellerHelper helper = getAlltelpersonbrandReport(year, month, storeid);
                result = helper.SellerReports.FirstOrDefault(t => t.Seller.Id == sellerid);                
            }
            if (result == null)
            {
                DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
                SellerKpiService SellerKpiService = new SellerKpiService();
                StoreService StoreService = new StoreService();
                this.GetTranstion().Inject(SellerKpiService);
                this.GetTranstion().Inject(StoreService);

                result = new SellerPersonReport
                {
                    Year = year,
                    Month = month,
                    Seller = seller,
                    Day = dt.Day,
                    BrandReports = new List<SellerPersonBrandReport>(),
                    Store = StoreService.GetQuery().FirstOrDefault(t => t.Id == storeid),
                    SellerKpiMonthConfig = SellerKpiService.GetSellerKpiMonthConfig(year, month, storeid, true),
                    SellerPerformanceStandard = GetSellerPerformanceStandard(year, month, storeid)
                };
            }
            return result;
        }
        
        public SellerPersonBrandReport GetSellerPersonBrandReport(int year, int month, int storeid, int sellerid, int brandid)
        {
            SellerPersonBrandReport result = null;
            EmployeeService EmployeeService = new Service.EmployeeService();
            this.GetTranstion().Inject(EmployeeService);
            var seller = EmployeeService.GetQuery().FirstOrDefault(t => t.Id == sellerid);
            SellerHelper helper=null;
            if (seller.Type == EmployeeType.FieldSaler)
            {
                helper = getAllpersonbrandReport(year, month, storeid);
                result = helper.SellerBrandReports.FirstOrDefault(t => t.Seller.Id == sellerid && t.CarBrand.Id == brandid);
                
            }
            else if (seller.Type == EmployeeType.TelSaler)
            {
                helper = getAlltelpersonbrandReport(year, month, storeid);
                result = helper.SellerBrandReports.FirstOrDefault(t => t.Seller.Id == sellerid && t.CarBrand.Id == brandid);
                
            }
            if (result == null)
            {
                DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
                SellerKpiService SellerKpiService = new SellerKpiService();
                StoreService StoreService = new StoreService();
                CarService CarService = new CarService();
                this.GetTranstion().Inject(SellerKpiService);
                this.GetTranstion().Inject(StoreService);
                this.GetTranstion().Inject(CarService);

                result = new SellerPersonBrandReport
                {
                    Year = year,
                    Month = month,
                    Seller = seller,
                    Day = dt.Day,
                     CarBrand= CarService.GetCarBrandQuery().FirstOrDefault(x=>x.Id==brandid),
                     SellerKpiDayDatas=new List<Domain.CommonSellerKpi.CommonSellerKpiDayData>(),
                    SellerMonthConfig = SellerKpiService.GetSellerMonthConfig(year, month, storeid, sellerid,true),
                       StoreReport=helper.StoreReport,
                    Store = StoreService.GetQuery().FirstOrDefault(t => t.Id == storeid),
                    SellerKpiMonthConfig = SellerKpiService.GetSellerKpiMonthConfig(year, month, storeid, true),
                    SellerPerformanceStandard = GetSellerPerformanceStandard(year, month, storeid)
                };
            }
            return result;
        }
        #endregion


        private SellerHelper getAllpersonbrandReport(int year, int month, int storeid)
        {
            SellerHelper helper = new SellerHelper();
            SellerKpiService SellerKpiService = new SellerKpiService();
            StoreService StoreService = new StoreService();
            this.GetTranstion().Inject(SellerKpiService);
            this.GetTranstion().Inject(StoreService);
            var daydatas = SellerKpiService.GetSellerKpiDayDatas(year, month, storeid, EmployeeType.FieldSaler, true).ToList();
            var sellers = daydatas.GroupBy(t => t.Seller).Select(t => t.Key).ToList();
            var carbarnds = daydatas.GroupBy(t => t.CarBrand).Select(t => t.Key).ToList();
            var storeMonthConfig = SellerKpiService.GetSellerKpiMonthConfig(year, month, storeid, true);
            if (storeMonthConfig == null)
            {
                storeMonthConfig = new Domain.SellerKpi.SellerKpiMonthConfig
                {
                    Month = new DateTime(year, month,1), StoreId=storeid
                };
            }
            var sellerconfigs = SellerKpiService.GetSellerMonthConfig(year, month, storeid, EmployeeType.FieldSaler, true).ToList();
            var store=StoreService.GetQuery().FirstOrDefault(t=>t.Id==storeid);
            var stand = GetSellerPerformanceStandard(year, month, storeid);
            DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
            helper.StoreReport = new SellerReport
            {
                Year = dt.Year,
                Month = dt.Month,
                Day = dt.Day,
                Store = store,
                SellerPerformanceStandard = stand,
                SellerKpiMonthConfig = storeMonthConfig,
                SellerReports = new List<SellerPersonReport>()
            };

            List<SellerPersonBrandReport> reports = new List<SellerPersonBrandReport>();
            foreach (var seller in sellers)
            {
                SellerPersonReport spreport = new SellerPersonReport
                {
                     BrandReports=new List<SellerPersonBrandReport>(),
                     Year = dt.Year,
                     Month = dt.Month,
                     Day = dt.Day,
                     Store = store,
                     SellerPerformanceStandard = stand,
                     SellerKpiMonthConfig = storeMonthConfig,
                     Seller = seller                      
                };
                helper.StoreReport.SellerReports.Add(spreport);
                helper.SellerReports.Add(spreport);

                foreach (var brand in carbarnds)
                {
                    SellerPersonBrandReport report = new SellerPersonBrandReport();
                    report.Year = year;
                    report.Month = month;
                    
                    report.Day = dt.Day;
                    report.Seller = seller;
                    report.CarBrand = brand;
                    report.SellerKpiMonthConfig = storeMonthConfig;
                    report.SellerMonthConfig = sellerconfigs.FirstOrDefault(t => t.SellerId == seller.Id&&t.CarBrandId==brand.Id);
                    if (report.SellerMonthConfig == null)
                    {
                        report.SellerMonthConfig = new Domain.SellerKpi.SellerMonthConfig
                        {
                            Seller = seller,
                            CarBrand = brand,
                            SellerId = seller.Id,
                            CarBrandId = brand.Id,
                            Month = new DateTime(year, month, report.Day)
                        };
                    }
                    report.StoreReport = helper.StoreReport;
                    report.SellerPerformanceStandard = stand;
                    report.SellerKpiDayDatas = daydatas.Where(t => t.SellerId == seller.Id && t.CarBrandId == brand.Id).Cast<S4Store.Domain.CommonSellerKpi.CommonSellerKpiDayData>().ToList();
                    spreport.BrandReports.Add(report);
                    helper.SellerBrandReports.Add(report);
                }
            }
            return helper;
        }

        private SellerHelper getAlltelpersonbrandReport(int year, int month, int storeid)
        {
            SellerHelper helper = new SellerHelper();
            TelSellerKpiService SellerKpiService = new TelSellerKpiService();
            StoreService StoreService = new StoreService();
            this.GetTranstion().Inject(SellerKpiService);
            this.GetTranstion().Inject(StoreService);
            var daydatas = SellerKpiService.GetSellerKpiDayDatas(year, month, storeid, EmployeeType.TelSaler, true).ToList();
            var sellers = daydatas.GroupBy(t => t.Seller).Select(t => t.Key).ToList();
            var carbarnds = daydatas.GroupBy(t => t.CarBrand).Select(t => t.Key).ToList();
            var storeMonthConfig = SellerKpiService.GetSellerKpiMonthConfig(year, month, storeid, true);
            if (storeMonthConfig == null)
            {
                storeMonthConfig =new Domain.TelSellerKpi.TelSellerKpiMonthConfig
                {
                    Month = new DateTime(year, month, 1),
                    StoreId = storeid
                };
            }
            var sellerconfigs = SellerKpiService.GetSellerMonthConfig(year, month, storeid, EmployeeType.TelSaler, true).ToList();
            var store = StoreService.GetQuery().FirstOrDefault(t => t.Id == storeid);
            var stand = GetSellerPerformanceStandard(year, month, storeid);
            DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
            helper.StoreReport = new SellerReport
            {
                Year = dt.Year,
                Month = dt.Month,
                Day = dt.Day,
                Store = store,
                SellerPerformanceStandard = stand,
                SellerKpiMonthConfig = storeMonthConfig,
                SellerReports = new List<SellerPersonReport>()
            };

            List<SellerPersonBrandReport> reports = new List<SellerPersonBrandReport>();
            foreach (var seller in sellers)
            {
                SellerPersonReport spreport = new SellerPersonReport
                {
                    BrandReports = new List<SellerPersonBrandReport>(),
                    Year = dt.Year,
                    Month = dt.Month,
                    Day = dt.Day,
                    Store = store,
                    SellerPerformanceStandard = stand,
                    SellerKpiMonthConfig = storeMonthConfig,
                    Seller = seller
                };
                helper.StoreReport.SellerReports.Add(spreport);
                helper.SellerReports.Add(spreport);

                foreach (var brand in carbarnds)
                {
                    SellerPersonBrandReport report = new SellerPersonBrandReport();
                    report.Year = year;
                    report.Month = month;

                    report.Day = dt.Day;
                    report.Seller = seller;
                    report.CarBrand = brand;
                    report.SellerKpiMonthConfig = storeMonthConfig;
                    report.SellerMonthConfig = sellerconfigs.FirstOrDefault(t => t.SellerId == seller.Id && t.CarBrandId == brand.Id);
                    if (report.SellerMonthConfig == null)
                    {
                        report.SellerMonthConfig = new Domain.SellerKpi.SellerMonthConfig
                        {
                            Seller = seller,
                            CarBrand = brand,
                            SellerId = seller.Id,
                            CarBrandId = brand.Id,
                            Month = new DateTime(year, month, report.Day)
                        };
                    }
                    report.StoreReport = helper.StoreReport;
                    report.SellerPerformanceStandard = stand;
                    report.SellerKpiDayDatas = daydatas.Where(t => t.SellerId == seller.Id && t.CarBrandId == brand.Id).Cast<S4Store.Domain.CommonSellerKpi.CommonSellerKpiDayData>().ToList();
                    spreport.BrandReports.Add(report);
                    helper.SellerBrandReports.Add(report);
                }
            }
            return helper;
        }

        private SellerHelper getFullpersonbrandReport(int year, int month, int storeid)
        {
            SellerHelper helper = new SellerHelper();
            AllSellerKpiService SellerKpiService = new AllSellerKpiService();
            StoreService StoreService = new StoreService();
            this.GetTranstion().Inject(SellerKpiService);
            this.GetTranstion().Inject(StoreService);
            var daydatas = SellerKpiService.GetSellerKpiDayDatas(year, month, storeid, EmployeeType.TelSaler| EmployeeType.FieldSaler, true).ToList();
            var sellers = daydatas.GroupBy(t => t.Seller).Select(t => t.Key).ToList();
            var carbarnds = daydatas.GroupBy(t => t.CarBrand).Select(t => t.Key).ToList();
            var storeMonthConfig = SellerKpiService.GetSellerKpiMonthConfig(year, month, storeid, true);
            if (storeMonthConfig == null)
            {
                storeMonthConfig = new CommonSellerKpiMonthConfig
                {
                    Month = new DateTime(year, month, 1),
                    StoreId = storeid
                };
            }
            var sellerconfigs = SellerKpiService.GetSellerMonthConfig(year, month, storeid, EmployeeType.TelSaler | EmployeeType.FieldSaler, true).ToList();
            var store = StoreService.GetQuery().FirstOrDefault(t => t.Id == storeid);
            var stand = GetSellerPerformanceStandard(year, month, storeid);
            DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
            helper.StoreReport = new SellerReport
            {
                Year = dt.Year,
                Month = dt.Month,
                Day = dt.Day,
                Store = store,
                SellerPerformanceStandard = stand,
                SellerKpiMonthConfig = storeMonthConfig,
                SellerReports = new List<SellerPersonReport>()
            };

            List<SellerPersonBrandReport> reports = new List<SellerPersonBrandReport>();
            foreach (var seller in sellers)
            {
                SellerPersonReport spreport = new SellerPersonReport
                {
                    BrandReports = new List<SellerPersonBrandReport>(),
                    Year = dt.Year,
                    Month = dt.Month,
                    Day = dt.Day,
                    Store = store,
                    SellerPerformanceStandard = stand,
                    SellerKpiMonthConfig = storeMonthConfig,
                    Seller = seller
                };
                helper.StoreReport.SellerReports.Add(spreport);
                helper.SellerReports.Add(spreport);

                foreach (var brand in carbarnds)
                {
                    SellerPersonBrandReport report = new SellerPersonBrandReport();
                    report.Year = year;
                    report.Month = month;

                    report.Day = dt.Day;
                    report.Seller = seller;
                    report.CarBrand = brand;
                    report.SellerKpiMonthConfig = storeMonthConfig;
                    report.SellerMonthConfig = sellerconfigs.FirstOrDefault(t => t.SellerId == seller.Id && t.CarBrandId == brand.Id);
                    if (report.SellerMonthConfig == null)
                    {
                        report.SellerMonthConfig = new S4Store.Domain.CommonSellerKpi.CommonSellerMonthConfig
                        {
                            Seller = seller,
                            CarBrand = brand,
                            SellerId = seller.Id,
                            CarBrandId = brand.Id,
                            Month = new DateTime(year, month, report.Day)
                        };
                    }
                    report.StoreReport = helper.StoreReport;
                    report.SellerPerformanceStandard = stand;
                    report.SellerKpiDayDatas = daydatas.Where(t => t.SellerId == seller.Id && t.CarBrandId == brand.Id).ToList();
                    spreport.BrandReports.Add(report);
                    helper.SellerBrandReports.Add(report);
                }
            }
            return helper;
        }

        private class SellerHelper
        {
            public SellerHelper()
            {
                SellerReports = new List<SellerPersonReport>();
                SellerBrandReports = new List<SellerPersonBrandReport>();
            }
            public SellerReport StoreReport { get; set; }
            public List<SellerPersonReport> SellerReports { get; set; }
            public List<SellerPersonBrandReport> SellerBrandReports { get; set; }
        }

        private CarHelper getCarHelper(int year, int month, int storeid)
        {
            CarHelper helper = new CarHelper();
            CarKpiService CarKpiService = new CarKpiService();
            StoreService StoreService = new StoreService();
            this.GetTranstion().Inject(CarKpiService);
            this.GetTranstion().Inject(StoreService);
            var daydatas = CarKpiService.GetCarKpiDayDatas(year, month, storeid,  true).ToList();
            var carbarnds = daydatas.GroupBy(t => t.CarModel.Brand).Select(t => t.Key).ToList();
            var carmodels=daydatas.GroupBy(t => t.CarModel).Select(t => t.Key).ToList();
            var storeMonthConfig = CarKpiService.GetCarKpiMonthConfig(year, month, storeid, true);
            var sellerstand = GetSellerPerformanceStandard(year, month, storeid);
            if (storeMonthConfig == null)
            {
                storeMonthConfig = new CarKpiMonthConfig
                {
                    Month = new DateTime(year, month, 1),
                    StoreId = storeid
                };
            }
            var modelconfigs = CarKpiService.GetCarModelMonthConfigs(year, month, storeid,true).ToList();
            var store = StoreService.GetQuery().FirstOrDefault(t => t.Id == storeid);
            DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
            helper.StoreReport = new CarSaleReport
            {
                Year = dt.Year,
                Month = dt.Month,
                Day = dt.Day,
                Store = store,
                CarKpiMonthConfig = storeMonthConfig,
                CarBrandSaleReports=new List<CarBrandSaleReport>(),
                SellerPerformanceStandard = sellerstand
            };

            List<CarBrandSaleReport> reports = new List<CarBrandSaleReport>();
            foreach (var brand in carbarnds)
            {
                CarBrandSaleReport brandreport = new CarBrandSaleReport
                {
                    CarModelSaleReports=new List<CarModelSaleReport>(),
                    CarBrand=brand,
                    CarKpiMonthConfig=storeMonthConfig,
                    Year = dt.Year,
                    Month = dt.Month,
                    Day = dt.Day,
                    Store = store,
                    CarModelMonthConfigs = modelconfigs.Where(t => t.CarModel.BrandId == brand.Id).ToList(),
                    SellerPerformanceStandard = sellerstand
                };
                helper.StoreReport.CarBrandSaleReports.Add(brandreport);
                helper.CarBrandReports.Add(brandreport);

                var models = carmodels.Where(t => t.BrandId == brand.Id).ToList();
                foreach (var model in models)
                {
                    CarModelSaleReport report = new CarModelSaleReport();
                    report.Year = year;
                    report.Month = month;

                    report.Day = dt.Day;
                    report.CarModel = model;
                    report.CarKpiMonthConfig = storeMonthConfig;
                    report.CarModelMonthConfig = modelconfigs.FirstOrDefault(t => t.CarModelId == model.Id );
                    report.SellerPerformanceStandard = sellerstand;
                    if (report.CarModelMonthConfig == null)
                    {
                        report.CarModelMonthConfig =new CarModelMonthConfig
                        {
                             CarModel=model,
                              CarModelId=model.Id, 
                              Store= store,
                              StoreId=storeid,
                            Month = new DateTime(year, month, report.Day)
                        };
                    }
                    report.StoreReport = helper.StoreReport;
                    report.CarKpiDayDatas = daydatas.Where(t => t.CarModelId == model.Id ).ToList();
                    brandreport.CarModelSaleReports.Add(report);
                    helper.CarModelReports.Add(report);
                }
            }
            return helper;
        }

        private class CarHelper
        {
            public CarHelper()
            {
                CarBrandReports = new List<CarBrandSaleReport>();
                CarModelReports = new List<CarModelSaleReport>();
            }
            public CarSaleReport StoreReport { get; set; }
            public List<CarBrandSaleReport> CarBrandReports { get; set; }
            public List<CarModelSaleReport> CarModelReports { get; set; }
        }
    }
}
